﻿using MyMusicPlay._4._0.ElementFactory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace MyMusicPlay._4._0
{
    public class Rander : BaseClass
    {
        private MainWindow _mainWindow { get; set; }

        private RightMenu _rightMenu { get; set; }

        private ElementFactoryBase _elementFactoryBase { get; set; }

        public Rander(MainWindow main)
        {
            _mainWindow = main;
        }

        public void Initialize()
        {
            _rightMenu = PlayApplication.GetModule<RightMenu>();
            _elementFactoryBase = PlayApplication.GetModule<ElementFactoryBase>();
        }

        #region MusicLists
        /// <summary>
        /// 渲染视图[新添] 歌单列表
        /// </summary>
        public void RenderViewOfMusicLists(string name)
        {
            var tb = _elementFactoryBase.Create<TextBlock>(textblock =>
            {
                textblock.Text = name;
                textblock.MouseLeftButtonUp += _mainWindow.MusicList_MouseUp;
                textblock.ContextMenu = _rightMenu.CreateMenuByMusicList(name);
                textblock.Cursor = Cursors.Hand;
            }, true, true);

            _mainWindow.OptionBar.Items.Add(tb);
        }

        /// <summary>
        /// 渲染视图[全部] 歌单列表
        /// </summary>
        public void RenderViewOfMusicLists(Dictionary<string, string> allMusicList)
        {
            var items = _mainWindow.OptionBar.Items;
            for (int i = 4; i != items.Count;)//这个地方的数字有问题
            {
                items.RemoveAt(i);
            }
            foreach (var item in allMusicList.Keys)
            {
                RenderViewOfMusicLists(item);
            }
        }
        #endregion

        #region MusicList
        /// <summary>
        /// 渲染视图 歌曲列表
        /// </summary>
        public void RenderViewOfMusicList(IEnumerable<Music> list)
        {
            _mainWindow.MusicDisplayList.Items.Clear();
            foreach (var item in list)
            {
                ListViewItem listViewItem = _elementFactoryBase.Create<ListViewItem>((listItem) =>
                {
                    listItem.Content = new { Code = item.Key, Name = item.Name, Author = item.Singer };
                });
                _mainWindow.MusicDisplayList.Items.Add(listViewItem);
            }
        }
        #endregion

        #region Disposition
        /// <summary>
        /// 这个地方是实时读取的,需要更改放到内存里
        /// </summary>
        public void RenderView()
        {
            _mainWindow.Disposition_ListBox.Items.Clear();
            RenderViewOfDispToMusicLists();
            RenderViewOfDispToBackImage();
            ReaderViewOfBackImageEffect();
            ReaderViewOfBackgroundColor();
            ReaderViewOfBackgroundOpacity();
            RenderViewOfDispToMusicPlayingBoardBackImage();
            ReaderViewOfDispToMusicPlayingBoardOpacity();

            _mainWindow.Disposition_ListBox.Items.Add(new StackPanel() { Height = 30 });
        }

        /// <summary>
        /// 渲染歌单配置
        /// </summary>
        public void RenderViewOfDispToMusicLists()
        {
            TextBlock title = new TextBlock();
            title.Margin = new Thickness(10, 10, 0, 0);
            title.Text = "Music";
            _mainWindow.Disposition_ListBox.Items.Add(title);

            //读取文件信息
            var musicLists = PlayApplication.GetModule<DispositionApp>().ReadDispositionOfMusicLists();
            int i = 0;
            foreach (var item in musicLists)
            {
                if (string.IsNullOrEmpty(item.Name) || string.IsNullOrEmpty(item.Path))
                    continue;
                StackPanel sp = new StackPanel()
                {
                    Orientation = Orientation.Horizontal,
                    Margin = new Thickness(40, 0, 10, 0),
                    Name = $"DispStackP_{i}"
                };
                TextBox tbName = new TextBox()
                {
                    Name = $"DispName_{i}",
                    Width = 100,
                    Text = item.Name
                };
                TextBox tbPath = new TextBox()
                {
                    Name = $"DispPath_{i}",
                    Width = 200,
                    Text = item.Path,
                    Margin = new Thickness(40, 0, 20, 0)
                };
                Button but = new Button()
                {
                    Name = $"DispButOfML_{i}",
                    Width = 100,
                    Height = 30,
                    Content = "Update",
                };
                but.Click += _mainWindow.UpdateDispositionOfMusicLists_Click;

                sp.Children.Add(tbName);
                sp.Children.Add(tbPath);
                sp.Children.Add(but);
                _mainWindow.Disposition_ListBox.Items.Add(sp);

                try
                {
                    sp.RegisterName(tbName.Name, tbName);
                    sp.RegisterName(tbPath.Name, tbPath);
                    sp.RegisterName(but.Name, tbName);
                    _mainWindow.Disposition_ListBox.RegisterName(sp.Name, sp);
                }
                catch (Exception ex)
                { }
                i++;
            }
        }
        /// <summary>
        /// 渲染背景图片配置
        /// </summary>
        public void RenderViewOfDispToBackImage()
        {
            TextBlock title = new TextBlock();
            title.Margin = new Thickness(10, 10, 0, 0);
            title.Text = "Background Image";
            _mainWindow.Disposition_ListBox.Items.Add(title);

            var backImage = PlayApplication.GetModule<DispositionApp>().ReadDispositionOfImage();
            StackPanel sp = new StackPanel()
            {
                Orientation = Orientation.Horizontal,
                Margin = new Thickness(40, 0, 10, 0),
                Name = "TitleImage"
            };
            TextBox tbPath = new TextBox()
            {
                Width = 340,
                Text = backImage.Path,
                Margin = new Thickness(0, 0, 20, 0),
                Name = "TitleImageTextBox"
            };
            Button but = new Button()
            {
                Width = 100,
                Height = 30,
                Content = "Update",
            };

            but.Click += _mainWindow.UpdateDispositionOfBackImage_Click;

            sp.Children.Add(tbPath);
            sp.Children.Add(but);
            _mainWindow.Disposition_ListBox.Items.Add(sp);

            try
            {
                sp.RegisterName(tbPath.Name, tbPath);
                _mainWindow.Disposition_ListBox.RegisterName(sp.Name, sp);
            }
            catch (Exception ex)
            { }
        }
        /// <summary>
        /// 渲染背景的高斯模糊进度条
        /// </summary>
        public void ReaderViewOfBackImageEffect()
        {
            TextBlock title = new TextBlock();
            title.Margin = new Thickness(10, 10, 0, 0);
            title.Text = "Background Image Effect";
            _mainWindow.Disposition_ListBox.Items.Add(title);

            var backImage = PlayApplication.GetModule<DispositionApp>().ReadDispositionOfImage();
            StackPanel sp = new StackPanel()
            {
                Orientation = Orientation.Horizontal,
                Margin = new Thickness(40, 0, 10, 0),
                Name = "TitleImage"
            };
            Slider sli = new Slider()
            {
                Name = "BackImageEffect_Slider",
                Width = 450,
                Maximum = 100,
                Minimum = 0,
                Value = backImage.Effect
            };

            sli.ValueChanged += _mainWindow.BackImageEffectSlider_ValueChanged;
            sli.LostMouseCapture += _mainWindow.BackImageEffectSlider_LostMouseCapture;

            sp.Children.Add(sli);
            _mainWindow.Disposition_ListBox.Items.Add(sp);
        }
        /// <summary>
        /// 渲染背景颜色的进度条
        /// </summary>
        public void ReaderViewOfBackgroundColor()
        {
            TextBlock title = new TextBlock();
            title.Margin = new Thickness(10, 10, 0, 0);
            title.Text = "Background Color";
            _mainWindow.Disposition_ListBox.Items.Add(title);

            var rgb = PlayApplication.GetModule<DispositionApp>().ReadDispositionOfBackgrounColor();
            WrapPanel sp = new WrapPanel()
            {
                Orientation = Orientation.Horizontal,
                Margin = new Thickness(40, 0, 10, 0),
                Width = 300
            };
            Slider sliR = new Slider()
            {
                Width = 255,
                Maximum = 255,
                Minimum = 0,
                Value = rgb.R,
                Margin = new Thickness(0, 0, 30, 10)
            };
            TextBlock tbR = new TextBlock() { Text = "R" };
            Slider sliG = new Slider()
            {
                Width = 255,
                Maximum = 255,
                Minimum = 0,
                Value = rgb.G,
                Margin = new Thickness(0, 0, 30, 10)
            };
            TextBlock tbG = new TextBlock() { Text = "G" };
            Slider sliB = new Slider()
            {
                Width = 255,
                Maximum = 255,
                Minimum = 0,
                Value = rgb.B,
                Margin = new Thickness(0, 0, 30, 0)
            };
            TextBlock tbB = new TextBlock() { Text = "B" };

            sp.Children.Add(sliR);
            sp.Children.Add(tbR);
            sp.Children.Add(sliG);
            sp.Children.Add(tbG);
            sp.Children.Add(sliB);
            sp.Children.Add(tbB);

            sliR.LostMouseCapture += _mainWindow.BackColorR_LostMouseCapture;
            sliR.ValueChanged += _mainWindow.BackColorR_ValueChanged;
            sliG.LostMouseCapture += _mainWindow.BackColorG_LostMouseCapture;
            sliG.ValueChanged += _mainWindow.BackColorG_ValueChanged;
            sliB.LostMouseCapture += _mainWindow.BackColorB_LostMouseCapture;
            sliB.ValueChanged += _mainWindow.BackColorB_ValueChanged;

            _mainWindow.Disposition_ListBox.Items.Add(sp);
        }
        /// <summary>
        /// 渲染背景颜色透明度
        /// </summary>
        public void ReaderViewOfBackgroundOpacity()
        {
            TextBlock title = new TextBlock();
            title.Margin = new Thickness(10, 10, 0, 0);
            title.Text = "Background Opacity";
            _mainWindow.Disposition_ListBox.Items.Add(title);

            var opacity = PlayApplication.GetModule<DispositionApp>().ReadDispositionOfBackOpacity();
            StackPanel sp = new StackPanel()
            {
                Orientation = Orientation.Horizontal,
                Margin = new Thickness(40, 0, 10, 0),
            };
            Slider sli = new Slider()
            {
                Width = 450,
                Maximum = 1,
                Minimum = 0,
                Value = opacity
            };

            sli.ValueChanged += _mainWindow.BackImageOpacitySlider_ValueChanged;
            sli.LostMouseCapture += _mainWindow.BackImageOpacitySlider_LostMouseCapture;

            sp.Children.Add(sli);
            _mainWindow.Disposition_ListBox.Items.Add(sp);
        }
        public void RenderViewOfDispToMusicPlayingBoardBackImage()
        {
            TextBlock title = new TextBlock();
            title.Margin = new Thickness(10, 10, 0, 0);
            title.Text = "MusicPlayingBoard Image";
            _mainWindow.Disposition_ListBox.Items.Add(title);

            var backImage = PlayApplication.GetModule<DispositionApp>().ReadDispositionOfMusicPlayingBoardBackColor();
            StackPanel sp = new StackPanel()
            {
                Orientation = Orientation.Horizontal,
                Margin = new Thickness(40, 0, 10, 0),
                Name = "TitleMusicPlayingBoardImage"
            };
            TextBox tbPath = new TextBox()
            {
                Width = 340,
                Text = backImage.Path,
                Margin = new Thickness(0, 0, 20, 0),
                Name = "TitleMusicPlayingBoardImageTextBox"
            };
            Button but = new Button()
            {
                Width = 100,
                Height = 30,
                Content = "Update",
            };

            but.Click += _mainWindow.UpdateDispositionOfMusicPlayingBoardBackImage_Click;

            sp.Children.Add(tbPath);
            sp.Children.Add(but);
            _mainWindow.Disposition_ListBox.Items.Add(sp);

            try
            {
                sp.RegisterName(tbPath.Name, tbPath);
                _mainWindow.Disposition_ListBox.RegisterName(sp.Name, sp);
            }
            catch (Exception ex)
            { }
        }
        public void ReaderViewOfDispToMusicPlayingBoardOpacity()
        {
            TextBlock title = new TextBlock();
            title.Margin = new Thickness(10, 10, 0, 0);
            title.Text = "MusicPlayingBoard Opacity";
            _mainWindow.Disposition_ListBox.Items.Add(title);

            var opacity = PlayApplication.GetModule<DispositionApp>().ReadDispositionOfMusicPlayingBoardBackColor().Opacity;
            StackPanel sp = new StackPanel()
            {
                Orientation = Orientation.Horizontal,
                Margin = new Thickness(40, 0, 10, 0),
            };
            Slider sli = new Slider()
            {
                Width = 450,
                Maximum = 1,
                Minimum = 0,
                Value = opacity
            };
            sli.ValueChanged += _mainWindow.MusicPlayingBoardOpacitySlider_ValueChanged;
            sli.LostMouseCapture += _mainWindow.MusicPlayingBoardOpacitySlider_LostMouseCapture;

            sp.Children.Add(sli);
            _mainWindow.Disposition_ListBox.Items.Add(sp);
        }

        #endregion

        #region MusicPlayingBoard
        public Border CreateItemByMusicPlayingBoard(Music item, int key)
        {
            if (item == null)
                return null;
            Border border = new() { BorderThickness = new Thickness(0, 2, 0, 2), Background = SY_Library.Color.BackColor(255, 0), Name = "Music_" + key };
            border.MouseEnter += _mainWindow.PlayBoard_MouseEnter;
            border.MouseLeave += _mainWindow.PlayBoard_MouseLeave;
            border.MouseLeftButtonUp += _mainWindow.PlayBoard_DoubleClick;
            border.ContextMenu = _rightMenu.CreateMenuByMusicPlayingBoard(key, item);
            DockPanel dp = new() { Name = $"playBoardItem_{key}", Margin = new Thickness(10, 5, 5, 5) };
            StackPanel sp01 = new() { MaxWidth = 170, Height = 35 };

            var tb01 = _elementFactoryBase.Create<TextBlock>(textblock =>
            {
                textblock.Name = "MusicPlayListByName_" + key;
                textblock.Text = item.Name;
                textblock.VerticalAlignment = VerticalAlignment.Top;
                textblock.FontSize = 15;
            }, true, true);

            var tb02 = _elementFactoryBase.Create<TextBlock>(textblock =>
           {
               textblock.Name = "MusicPlayListBySinger_" + key;
               textblock.Text = item.Singer;
               textblock.VerticalAlignment = VerticalAlignment.Bottom;
           }, true, true);

            var tb03 = _elementFactoryBase.Create<TextBlock>(textblock =>
            {
                textblock.Text = "Del";
                textblock.HorizontalAlignment = HorizontalAlignment.Right;
                textblock.VerticalAlignment = VerticalAlignment.Center;
            }, true, true);

            tb03.Cursor = Cursors.Hand;
            tb03.MouseLeftButtonUp += _mainWindow.PlayBoardItemDel_MouseUp;
            var _media = PlayApplication.GetModule<Media>();
            if (key == _media.CurPlayingKey)
                border.BorderBrush = SY_Library.Color.BackColor(103, 58, 183);

            sp01.Children.Add(tb01);
            sp01.Children.Add(tb02);
            dp.Children.Add(sp01);
            dp.Children.Add(tb03);
            border.Child = dp;

            return border;
        }

        public void CreatePlayingBoardByTop(StackPanel sp, Music music)
        {
            sp.Children.Clear();
            var disTextColor = PlayApplication.GetModule<DispositionApp>().ReadDispositionOfTextColor();
            var color = SY_Library.Color.BackColor(disTextColor.R, disTextColor.G, disTextColor.B);
            var t1 = _elementFactoryBase.Create<TextBlock>(text =>
            {
                text.VerticalAlignment = VerticalAlignment.Top; text.FontSize = 15;
                text.Text = music.Name;
            }, true, true);
            var t2 = _elementFactoryBase.Create<TextBlock>(text =>
            {
                text.VerticalAlignment = VerticalAlignment.Bottom; text.HorizontalAlignment = HorizontalAlignment.Left; text.Margin = new Thickness(5, 0, 0, 0);
                text.Text = music.Singer;
            }, true, true);
            sp.Children.Add(t1);
            sp.Children.Add(t2);
        }
        #endregion

        #region MessageBox
        /// <summary>
        /// 创建默认模板
        /// </summary>
        public void CreateDefaultBoxTemplate(string text)
        {
            var MyMessage = PlayApplication.GetModule<MyMessageBox>();
            MyMessage.messageBox.Children.Clear();

            TextBlock textBlock01 = new() { Text = "REMIND!", Foreground = SY_Library.Color.BackColor(165, 255 / 5 * 4) };
            WpfMultiStyle.Apply.SetMultiStyle(textBlock01, "MaterialDesignHeadline6TextBlock");
            Grid grid = new() { Height = 125, Width = 280, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
            TextBlock textBlock02 = new() { Text = text, Foreground = SY_Library.Color.BackColor(165, 255 / 5 * 4), FontSize = 17, LineHeight = 25, TextWrapping = TextWrapping.Wrap, VerticalAlignment = VerticalAlignment.Center };
            WpfMultiStyle.Apply.SetMultiStyle(textBlock02, "MaterialDesignHeadline6TextBlock");
            Button but = new() { HorizontalAlignment = HorizontalAlignment.Right, Content = "Yes!" };
            WpfMultiStyle.Apply.SetMultiStyle(but, "MaterialDesignFlatButton");
            but.Click += MyMessage.YesButton_Click;

            MyMessage.messageBox.Children.Add(textBlock01);
            MyMessage.messageBox.Children.Add(grid);
            grid.Children.Add(textBlock02);
            MyMessage.messageBox.Children.Add(but);
        }

        public FrameworkElement CreateUpdayeMusicBox(Music music, int key)
        {
            var factory = PlayApplication.GetModule<ElementFactoryBase>();

            var str = factory.Create<StackPanel>();
            var tb = factory.Create<TextBlock>(text => { text.Text = "UPDATE"; text.Foreground = SY_Library.Color.BackColor(128); }, false);
            var textbox01 = factory.Create<TextBox>(box =>
            {
                box.Name = "UpdateMusicBox_Name"; box.Width = 230; box.Margin = new Thickness(0, 25, 0, 10); box.Text = music.Name; box.TextAlignment = TextAlignment.Center; box.FontSize = 14;
            });
            var textbox02 = factory.Create<TextBox>(box =>
            {
                box.Name = "UpdateMusicBox_Singer"; box.Width = 230; box.Margin = new Thickness(10); box.Text = music.Singer; box.TextAlignment = TextAlignment.Center; box.FontSize = 14;
            });
            var botton = factory.Create<Button>(but => { but.Name = "UpdateMusicButIsKey_" + key; but.Width = 100; but.Margin = new Thickness(0, 19, 0, 10); but.Content = "OK"; });
            WpfMultiStyle.Apply.SetMultiStyle(botton, "MaterialDesignFlatButton");
            botton.Click += _mainWindow.MessageBoxByUpdateMusic_Click;

            str.Children.Add(tb);
            str.Children.Add(textbox01);
            str.Children.Add(textbox02);
            str.Children.Add(botton);

            return str;
        }
        #endregion

        #region UpdateColor
        public void UpdateColorByForeground(ColorJson json = null)
        {
            ColorJson disTextColor = json;
            if (json == null)
            {
                disTextColor = PlayApplication.GetModule<DispositionApp>().ReadDispositionOfTextColor();
            }
            var textBlockList = _elementFactoryBase.GetUpdateList<TextBlock>();
            textBlockList.ForEach(textBlock =>
            {
                (textBlock as TextBlock).Foreground = SY_Library.Color.BackColor(disTextColor.R, disTextColor.G, disTextColor.B);
            });
            var listBoxList = _elementFactoryBase.GetUpdateList<ListView>();
            listBoxList.ForEach(listBlock =>
            {
                (listBlock as ListView).Foreground = SY_Library.Color.BackColor(disTextColor.R, disTextColor.G, disTextColor.B);
            });
            var butList = _elementFactoryBase.GetUpdateList<Button>();
            butList.ForEach(but =>
            {
                (but as Button).Foreground = SY_Library.Color.BackColor(disTextColor.R, disTextColor.G, disTextColor.B);
            });
        }
        #endregion
    }
}
